/********************************************
 * 功能说明: LesseeInvoice实体与DTO之间接口数据转换实现
 * 模块名称:
 * 系统名称:
 * 软件版权: Frank
 * 系统版本: 1.0.0.1
 * 开发人员:
 * 开发时间: 2020-03-24 10:09:23
 * 审核人员:
 * 相关文档:
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.operation.tenant.web.converter;

import com.spring.arch.common.converter.AbstractConverter;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.common.converter.AbstractConverter;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.operation.tenant.errorcode.TenantErrorCode;
import com.spring.arch.operation.tenant.model.LesseeInvoice;
import com.spring.arch.operation.tenant.repository.LesseeInvoiceRepository;
import com.spring.arch.operation.tenant.web.dto.WebLesseeInvoiceDTO;
import lombok.NonNull;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;

/**
 * LesseeInvoice实体与DTO之间接口数据转换实现
 * @author
 * @version 1.0.0.1
 * @since 2020-03-24 10:09:23
 */
@Component
public class WebLesseeInvoiceConverter extends AbstractConverter<LesseeInvoice, WebLesseeInvoiceDTO> {

    @Autowired
    private LesseeInvoiceRepository lesseeInvoiceRepository;
    @Autowired
    private EntityManager entityManager;

    @Override
    public LesseeInvoice toModel(@NonNull final WebLesseeInvoiceDTO dto) {
        if (dto.isNew()) {
            return constructModel(dto);
        } else {
            return updateModel(dto);
        }
    }

    @Override
    public WebLesseeInvoiceDTO toDTO(@NonNull final LesseeInvoice model) {
        final WebLesseeInvoiceDTO dto = new WebLesseeInvoiceDTO();

        dto.setId(model.getId());
        dto.setCompanyName(model.getCompanyName());
        dto.setDutyNo(model.getDutyNo());
        dto.setRegisterAddress(model.getRegisterAddress());
        dto.setTelephone(model.getTelephone());
        dto.setOpeningBank(model.getOpeningBank());
        dto.setOpeningBankAccount(model.getOpeningBankAccount());
        dto.setDefaultInvoice(model.isDefaultInvoice());


        this.convertCommonToDTO(model, dto);

        return dto;
    }

    /**
     * 构建新Model
     */
    private LesseeInvoice constructModel(final WebLesseeInvoiceDTO dto) {
        final LesseeInvoice model = new LesseeInvoice();
        this.dtoToModel(dto, model);
        return model;
    }

     /**
      * 更新Model
      */
    private LesseeInvoice updateModel(final WebLesseeInvoiceDTO dto) {
        final LesseeInvoice model = this.lesseeInvoiceRepository.findById(dto.getId())
                        .orElseThrow(() -> ExceptionHolder.serviceException(TenantErrorCode.E1201000001, dto.getId().toString()));
        entityManager.unwrap(Session.class).evict(model);
        this.dtoToModel(dto, model);
        return model;
    }

    private void dtoToModel(final WebLesseeInvoiceDTO dto, final LesseeInvoice model) {
        model.setCompanyName(dto.getCompanyName());
        model.setDutyNo(dto.getDutyNo());
        model.setRegisterAddress(dto.getRegisterAddress());
        model.setTelephone(dto.getTelephone());
        model.setOpeningBank(dto.getOpeningBank());
        model.setOpeningBankAccount(dto.getOpeningBankAccount());
        model.setDefaultInvoice(dto.isDefaultInvoice());

    }
}
